package com.bluexmicro.ota2.ui.screen.index

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bluexmicro.bluetooth.scan.ScannerImpl
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class IndexViewModel : ViewModel() {

    private val repo = VersionRepository()

    private val localInfo = MutableStateFlow(VersionInfo(0, "1.0.0"))
    val local: StateFlow<VersionInfo> = localInfo

    private val remoteInfo = MutableStateFlow<VersionInfo?>(null)
    val remote: StateFlow<VersionInfo?> = remoteInfo


    fun checkVersion(context: Context) = viewModelScope.launch(Dispatchers.Default) {
        localInfo.value = repo.localVersion(context)
        val info = repo.checkVersion()
        val remoteCode = info?.code ?: 0
        if (remoteCode > localInfo.value.code) {
            remoteInfo.value = info
        }
    }

}